package org.filecollector.msgcontroller.writer;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

class WriterStream implements BasicsWriter {
    OutputStream os;
    Charset charset;

    WriterStream(OutputStream os) {
        this.os = os;
        this.charset = StandardCharsets.UTF_16;
    }

    WriterStream(OutputStream os, Charset charset) {
        this.os = os;
        this.charset = charset;
    }

    @Override
    public synchronized boolean write(String message) {
        byte[] bytes = (message + "\0").getBytes(charset);
        try {
            os.write(bytes);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return true;
    }
}
